<?php

namespace App\Repository;

use App\Consts\GlobalConst;
use App\Consts\GlobalDynamicConst;
use App\Models\FeedDynamic;

/**
 * Class FeedDynamicRepository
 * @package App\Repository
 */
class FeedDynamicRepository extends BaseRepository
{

    /**
     * @param $loadData
     * @return FeedDynamic|bool|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function create($loadData)
    {
        $feedDynamic = $this->getModelByDynamicId($loadData['dynamic_id']);
        if ($feedDynamic) {
            $feedDynamic->is_deleted = GlobalConst::IS_NOT_DELETE;
            $feedDynamic->is_view = GlobalDynamicConst::IS_VIEW;
            $feedDynamic->save();
            return $feedDynamic->save() ? $feedDynamic : false;
        }
        $feedDynamic = FeedDynamic::getModel();
        return $feedDynamic->create($loadData) ?: false;
    }

    /**
     * @param $dynamicId
     * @return FeedDynamic|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getModelByDynamicId($dynamicId)
    {
        return FeedDynamic::getModel()
            ->where('dynamic_id', $dynamicId)
            ->first();
    }

    /**
     * @param $condition
     */
    public static function deleteByCondition($condition)
    {
        FeedDynamic::getModel()
            ->where($condition)
            ->update(['is_deleted' => GlobalConst::IS_NOT_DELETE]);
    }

}
